package com.unohacha.qqj.fzdr.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.huangmin66.rongcloud.RongCloudUtils;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.view.CropImageView;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.SimpleCallBack;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.bean.BaseBean;
import com.unohacha.qqj.fzdr.bean.MyImgLibDetial;
import com.unohacha.qqj.fzdr.imp.MyApplication;
import com.unohacha.qqj.fzdr.imp.MyCallBack;
import com.unohacha.qqj.fzdr.imp.UrlConfig;
import com.unohacha.qqj.fzdr.imp.XUtil;
import com.unohacha.qqj.fzdr.main.FindDetailActivity2;
import com.unohacha.qqj.fzdr.util.GlideImageLoader;
import com.unohacha.qqj.fzdr.util.GlideImageLoaderForPicker;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.fzdr.util.ToastUtils;
import com.unohacha.qqj.fzdr.view.ObservableScrollView;
import com.unohacha.qqj.mylibrary.base.BaseActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.UserInfo;

public class MyImgLibDetialActivity extends BaseActivity {
    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.home1_title)
    LinearLayout homeTile;
    @BindView(R.id.send_tv)
    TextView sendTv;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.common_right_text)
    TextView commonRightText;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_chengfen)
    TextView tvChengfen;
    @BindView(R.id.tv_kezhong)
    TextView tvKezhong;
    @BindView(R.id.tv_fukuan)
    TextView tvFukuan;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_is_open)
    TextView tvIsOpen;
    private int id;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.scrollView)
    ObservableScrollView scrollView;
    private int imageHeight;
    private int type = 1;
    private BaseBean<MyImgLibDetial> list;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_img_lib_detial;
    }

    @Override
    protected void initData() {
        super.initData();
        if(null != getIntent()){
            id=getIntent().getIntExtra("id",0);
            type = getIntent().getIntExtra("type",1);
        }
        commonTitle.setText("图组详情");
        commonRightImage.setVisibility(View.GONE);
        commonRightText.setVisibility(View.GONE);
        commonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        
        if(type == 2){
            sendTv.setText("立即编辑");
            sendTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Prefer.getInstance().setEditXH(list.getData().getTitle());//标题
                    Prefer.getInstance().setEditType(list.getData().getSupply_cate_id());//分类
                    Prefer.getInstance().setEditNature(list.getData().getTypeid()+"");//性质
                    Prefer.getInstance().setEditElement(list.getData().getComponent());//成分
                    Prefer.getInstance().setEditWeight(list.getData().getGram_weight()+list.getData().getGram_weight_company());//克重
                    Prefer.getInstance().setEditWidth(list.getData().getBreadth()+list.getData().getBreadth_company());//幅宽
                    Prefer.getInstance().setOtherContent(list.getData().getContent());
                    Intent intent = new Intent(MyImgLibDetialActivity.this,ImgUploadActivity.class);
                    intent.putExtra("upload_type",2);
                    intent.putExtra("fabricId",list.getData().getSupply_cate_id());
                    intent.putExtra("listPic",(Serializable) list.getData().getList_pic());
                    startActivity(intent);
                }
            });
        }else{
            sendTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RongCloudUtils.getInstance().startConversation(MyImgLibDetialActivity.this, Conversation.ConversationType.PRIVATE,Prefer.getInstance().getMember_id(),Prefer.getInstance().getNick());

                    getMyNews();
                }
            });
        }

        initListeners();
        Log.d("detaleInfo","id:"+id+" typeId:"+" "+Prefer.getInstance().getToken());
        SuperHttp.post("api/mycenter/myLibraryListDetail")
                .addParam("token", Prefer.getInstance().getToken())
                .addParam("typeid",type+"")
                .addParam("id",id+"")
                .request(new SimpleCallBack<BaseBean<MyImgLibDetial>>() {
                    @Override
                    public void onSuccess(BaseBean<MyImgLibDetial> data) {
                        list = data;
                        if(data.getCode()==1){
                            tvName.setText(data.getData().getTitle());
                            tvType.setText("分类："+data.getData().getSupply_cate_id());
                            if(data.getData().getTypeid()==3){
                                tvIsOpen.setText("公开");
                            }else {
                                tvIsOpen.setText("私密");
                            }
                            tvChengfen.setText(data.getData().getComponent());
                            tvKezhong.setText(data.getData().getGram_weight()+data.getData().getGram_weight_company());
                            tvFukuan.setText(data.getData().getBreadth()+data.getData().getBreadth_company());
                            tvContent.setText(data.getData().getContent());
                            setBannerData(data.getData().getList_pic());
                            
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }

    //获取个人信息
    private void getMyNews(){
        Map<String,Object> params = new HashMap<>();
        params.put("id",Prefer.getInstance().getMember_id());
        XUtil.Post(UrlConfig.MY_NEW_INFO,params,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (1 == jsonObject.optInt("code")){
                        Gson gson = new Gson();
                        RongCloudUtils.getInstance()
                                .setCurrentUserInfo(new UserInfo(Prefer.getInstance().getMember_id(), Prefer.getInstance().getNick(), Uri.parse(Prefer.getInstance().getMember_thumb())));
                    }else if (100 == jsonObject.optInt("code")){
                        MyToast(getResources().getString(R.string.login_out));
                        MyApplication.getInstance().gotoLoginActivity();
                    }else {
                        MyToast(jsonObject.optString("message"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    //实现标题栏随滚动的显示和隐藏
    private void initListeners() {
        // 获取顶部图片高度后，设置滚动监听
        ViewTreeObserver vto = banner.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                banner.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);
                imageHeight = banner.getHeight();
                scrollView.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
                    @Override
                    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
                        // TODO Auto-generated method stub
                        // Log.i("TAG", "y--->" + y + "    height-->" + height);
                        if (y <= 0) {
//                          设置文字背景颜色，白色
                            homeTile.setBackgroundColor(Color.argb((int) 0, 255, 255, 255));//AGB由相关工具获得，或者美工提供
//                          设置文字颜色，黑色
                            commonTitle.setTextColor(Color.argb((int) 0, 255, 255, 255));
                            Log.e("111","y <= 0");
                        } else if (y > 0 && y <= imageHeight) {

                            float scale = (float) y / imageHeight;
                            float alpha = (255 * scale);
                            // 只是layout背景透明(仿知乎滑动效果)白色透明
                            homeTile.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
                            //                          设置文字颜色，黑色，加透明度
                            commonTitle.setTextColor(Color.argb((int) alpha, 0, 0, 0));
                            Log.e("111","y > 0 && y <= imageHeight");
                        } else {
//							白色不透明
                            homeTile.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
                            //                          设置文字颜色
                            //黑色
                            commonTitle.setTextColor(Color.argb((int) 255, 0, 0, 0));
                            Log.e("111","else");
                        }

                    }
                });

            }
        });


    }



    //设置banner 数据
    private void setBannerData(List<String> mBannerList) {

            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);//设置圆形指示器与标题
            banner.setIndicatorGravity(BannerConfig.CENTER);//设置指示器位置
            banner.setImages(mBannerList);
            banner.setImageLoader(new GlideImageLoader());
            banner.start();

            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {
                    //用mBannerAllList集合
                }
            });
        }

}
